package org.kaer.election.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.kaer.election.application.ElectionVoteApplication;
import org.kaer.election.trade.ExampleClient;
import org.kaer.election.ui.BaseActivity;

import fynn.app.ProgressDialog;
import fynn.app.PromptDialog;

public abstract class BaseFragment extends Fragment {
	protected Context mContext;
	protected View layout;
	protected ElectionVoteApplication app = null;
	protected ExampleClient mExampleClient;
	protected Bundle mBundle;
	protected ProgressDialog progressDialog;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mExampleClient = ExampleClient.getInstance();
		mBundle=getArguments();
		if(mBundle==null)
			mBundle=new Bundle();

	}

	@Override
	public void onAttach(Context mContext) {
		// TODO Auto-generated method stub
		super.onAttach(mContext);
		this.mContext = mContext;
		app=(ElectionVoteApplication)((BaseActivity)mContext).getApplication();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		layout = inflater.inflate(getLayoutId(), container, false);
		return layout;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		initView(view, savedInstanceState);
	}

	protected View findView(int viewid) {
		return layout.findViewById(viewid);
	}
	//获取宿主Activity
	protected BaseActivity getHoldingActivity() {
		return (BaseActivity) mContext;
	}



	//添加fragment
	protected void addFragment(BaseFragment fragment) {
		if (null != fragment) {
			getHoldingActivity().addFragment(fragment);
		}
	}

	//移除fragment
	protected void removeFragment() {
		getHoldingActivity().removeFragment();
	}
	@Override
	public void onDestroy() {
		destroy();
		super.onDestroy();
	}
	protected void showPromptDialog(String message){
		getHoldingActivity().showPromptDialog(message);
	}
	protected void showPromptDialog(String message, PromptDialog.OnClickListener listener){
		getHoldingActivity().showPromptDialog(message,listener);
	}
	/**
	 * @param cls
	 */
	protected void startActivity(Class<?> cls) {
		startActivity(cls, null);
	}

	/**
	 * @param cls
	 * @param mBundle
	 */
	protected void startActivity(Class<?> cls, Bundle mBundle) {
		try {
			Intent intent = new Intent();
			intent.setClass(mContext, cls);
			if (mBundle != null) {
				intent.putExtras(mBundle);
			}
			mContext.startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void showProgressDialog(String message, Context context) {
		try {
			dismissDialog();
			progressDialog = new ProgressDialog.Builder(context).setMessage(message).setCancelable(true).create();
			progressDialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void showProgressDialog(String message) {

		showProgressDialog(message, mContext);
	}

	protected void dismissDialog() {
		if (progressDialog != null && progressDialog.isShowing())
			progressDialog.dismiss();

	}
	protected void destroy() {

	}
	protected void setMargins(View v, int l, int t, int r, int b) {
		if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
			ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
			p.setMargins(l, t, r, b);
			v.requestLayout();
		}
	}
	protected abstract int getLayoutId();

	protected abstract void initView(View view, Bundle savedInstanceState);
}
